/* Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 * ===================================================================================================================*/
#include "base/context_builder/op_infer_datatype_context_builder.h"
#include "base/context_builder/op_context_builder_impl.h"
#include "base/context_builder/context_holder_builder.h"
#include "common/ge_common/util.h"
#include "base/context_builder/op_info.h"
#include "base/context_builder/op_info_impl.h"
#include <memory>
#include "securec.h"
#include "graph/debug/ge_util.h"

namespace gert {
class OpInferDataTypeContextBuilderImpl : public ContextBuilderImpl {
 public:
  OpInferDataTypeContextBuilderImpl() : ContextBuilderImpl() {}
  ~OpInferDataTypeContextBuilderImpl() override = default;

  std::unique_ptr<ContextHolderImpl> BuildInferDataTypeContext() {
    auto op_info_impl = OpInfoHelper::GetPtr(op_info_);
    GE_ASSERT_NOTNULL(op_info_impl, "op_info_impl is nullptr");
    GE_ASSERT_TRUE(op_info_impl->CheckParams(), "CheckParams failed, op_type: %s, op_name: %s",
                   op_info_impl->op_type_.c_str(), op_info_impl->op_name_.c_str());
    auto holder = ge::ComGraphMakeUnique<ContextHolderImpl>();
    GE_ASSERT_NOTNULL(holder, "Create ContextHolderImpl failed.");
    GE_ASSERT_SUCCESS(CreateComputeNodeInfo(*holder), "Create compute node info failed.");
    GE_ASSERT_SUCCESS(BuildCtx(*holder), "BuildCtx failed.");
    auto origin_context = holder->GetContext<KernelContext>();
    if (use_data_type_ptr_) {
      for (size_t i = 0U; i < input_values_.size(); ++i) {
        memcpy_s(origin_context->MutableInputPointer<void *>(i), sizeof(void *), input_values_[i].first,
                 sizeof(ge::DataType));
      }
    }
    for (size_t i = 0U; i < output_values_.size(); ++i) {
      memcpy_s(origin_context->GetOutputPointer<void *>(i), sizeof(void *), output_values_[i].first,
               sizeof(ge::DataType));
    }
    return holder;
  }

  void Inputs(const std::vector<ge::DataType *> &inputs) {
    if (!use_data_type_ptr_) {
      input_values_.clear();
    }
    for (auto i : inputs) {
      input_values_.emplace_back(std::make_pair<void *, gert::Chain::Deleter>(i, nullptr));
    }
    use_data_type_ptr_ = true;
  }

  void Inputs(const std::vector<ge::DataType> &inputs) {
    if (use_data_type_ptr_) {
      input_values_.clear();
    }
    for (auto i : inputs) {
      input_values_.emplace_back(std::make_pair<void *, gert::Chain::Deleter>(ge::ValueToPtr(i), nullptr));
    }
    use_data_type_ptr_ = false;
  }

  void Outputs(const std::vector<ge::DataType *> &outputs) {
    for (auto output : outputs) {
      output_values_.emplace_back(std::make_pair<void *, gert::Chain::Deleter>(output, nullptr));
    }
  }
  bool use_data_type_ptr_ = false;  // Flag to indicate if input values are DataType pointers or values
};

OpInferDataTypeContextBuilder::OpInferDataTypeContextBuilder()
    : impl_(ge::ComGraphMakeUnique<OpInferDataTypeContextBuilderImpl>()) {}
OpInferDataTypeContextBuilder::~OpInferDataTypeContextBuilder() {};

OpInferDataTypeContextBuilder &OpInferDataTypeContextBuilder::Inputs(const std::vector<ge::DataType *> &inputs) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  static_cast<OpInferDataTypeContextBuilderImpl *>(impl_.get())->Inputs(inputs);
  return *this;
}

OpInferDataTypeContextBuilder &OpInferDataTypeContextBuilder::Inputs(const std::vector<ge::DataType> &inputs) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  static_cast<OpInferDataTypeContextBuilderImpl *>(impl_.get())->Inputs(inputs);
  return *this;
}

OpInferDataTypeContextBuilder &OpInferDataTypeContextBuilder::Outputs(const std::vector<ge::DataType *> &outputs) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  static_cast<OpInferDataTypeContextBuilderImpl *>(impl_.get())->Outputs(outputs);
  return *this;
}

ContextHolder<InferDataTypeContext> OpInferDataTypeContextBuilder::Build() {
  GE_CHECK_NOTNULL_EXEC(impl_, return ContextHolder<InferDataTypeContext>());
  auto ctx_holder_impl = static_cast<OpInferDataTypeContextBuilderImpl *>(impl_.get())->BuildInferDataTypeContext();
  auto holder_void = ContextHolderBuilder::Create(std::move(ctx_holder_impl));
  return ContextHolder<InferDataTypeContext>(std::move(holder_void));
}

OpInfo &OpInferDataTypeContextBuilder::MutableOpInfo() {
  static OpInfo null_op_info;
  GE_CHECK_NOTNULL_EXEC(impl_, return null_op_info);
  return impl_->GetOpInfo();
}

}  // namespace gert
